package com.aixin.project.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.Template;
import org.apache.velocity.context.Context;

import com.aixin.project.data.AxSession;
import com.aixin.project.data.CommonConstants;
import com.aixin.project.data.Course;
import com.aixin.project.data.CourseOrder;
import com.aixin.project.data.Expert;
import com.aixin.project.data.User;
import com.aixin.project.db.CourseOrderDao;
import com.aixin.project.handler.CourseHandler;
import com.aixin.project.handler.CourseOrderHandler;
import com.aixin.project.handler.ExpertHandler;
import com.aixin.project.handler.UserHandler;
import com.aixin.project.util.DateUtil;
import com.aixin.project.util.RequestUtil;

public class OrdersucsServlet extends BaseServlet {


	@Override
	protected Template execute(HttpServletRequest request,
			HttpServletResponse response, Context ctx) {
		
		AxSession axsession = (AxSession) request.getSession().getAttribute(CommonConstants.USER_SESSION);
		if (null == axsession) {
			this.sendRedirect(response, "/");
			return null;
		}
		
		long id = axsession.getUid();
		int cid = RequestUtil.getInt(request, "cid", -1);
		if (cid != -1) {
			Course course = CourseHandler.find(cid);
			Expert expert = ExpertHandler.find(course.getExpertid());
			User user=UserHandler.find(id);
			ctx.put("user", user);
			ctx.put("expert", expert);
			ctx.put("course", course);
			double totalPrice=RequestUtil.getDouble(request, "consume", 0.00);
			double price=RequestUtil.getDouble(request, "price", 0.00);
			int point=RequestUtil.getInt(request, "courseNum", -1);
			if(totalPrice!=0 && point!=-1){
				CourseOrder order=new CourseOrder();
				order.setCourseid(cid);
			    order.setExportid(expert.getId());
			    order.setPoint(point);
			    order.setPrice(totalPrice);
			    order.setUserid(id);
			    int status=  CourseOrderHandler.create(cid, id, order);
			    System.out.println("--------status---"+status);
				ctx.put("totalPrice",totalPrice);
				ctx.put("point", point);
			}else{
				ctx.put("error", "error");
			}
			
			
		}else{
			ctx.put("error", "error");
		}
	
		return getTemplate("ordersuc.vm");
	}

}
